package com.solution.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.solution.constant.LoginConstants;
import com.solution.constant.StatusConstants;
import com.solution.delegate.ExaminationServiceDelegate;
import com.solution.delegate.UserInfoServiceDelegate;
import com.solution.exception.DelegateException;
import com.solution.exception.ServiceBeanException;
import com.solution.form.ReportForm;
import com.solution.model.UserInfo;
import com.solution.util.SearchVO;


public class ReportAction extends Action {
	Logger logger = Logger.getLogger(ReportAction.class.getName());
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request, 
			HttpServletResponse response)throws IOException, ServletException{
		request.setAttribute("tab", "REPORT");
		request.setAttribute("examSummaryLink", "/examwiseReport.do");
		request.setAttribute("userDetailLink", "/questionwiseReport.do");
		ReportForm reportForm = (ReportForm)form;
		
		HttpSession session = request.getSession();
		try{
			UserInfo userInfo = (UserInfo)request.getSession().getAttribute(LoginConstants.SESSION_USER_INFO);
			Integer activeExams = 0;
			Integer activeExaminers = 0;
			Integer activeExaminees = 0;

			ExaminationServiceDelegate examinationServiceDelegate = new ExaminationServiceDelegate();
			UserInfoServiceDelegate userInfoServiceDelegate = new UserInfoServiceDelegate();
			
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
			searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId(), true);
			activeExams = examinationServiceDelegate.countExams(searchVO);
			
			searchVO.clear();
			searchVO.addEqualsCondition("status", LoginConstants.USER_ACTIVE);
			searchVO.addIsNullCondition("examType");
			searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId(), true);
			activeExaminers = userInfoServiceDelegate.countUser(searchVO);
			
			searchVO.clear();
			searchVO.addEqualsCondition("status", LoginConstants.USER_ACTIVE);
			searchVO.addIsNotNullCondition("examType");
			searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId(), true);
			activeExaminees = userInfoServiceDelegate.countUser(searchVO);
			
			session.setAttribute("ActiveExaminers", activeExaminers);
			session.setAttribute("ActiveExam", activeExams);
			session.setAttribute("ActiveExaminees", activeExaminees);
			
		}
		catch(ServiceBeanException sbe){
			logger.error(sbe);
		}
		catch(DelegateException de){
			logger.error(de);
		}
		
		if("/enrollmentReport".equals(mapping.getPath())){
			return enrollmentReport(mapping, reportForm, request, response);
		}else if("/examwiseReport".equals(mapping.getPath())){
			request.setAttribute("selectedTab", "ExamSummary");
			return  mapping.findForward("examwiseReport");
		}else if("/questionwiseReport".equals(mapping.getPath())){
			request.setAttribute("selectedTab", "UserDetail");
			return  mapping.findForward("questionwiseReport");
		}		
		return mapping.findForward("success");
	}
	
	
	private ActionForward enrollmentReport(ActionMapping mapping,ReportForm reportForm,
			HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		try {
			//reportForm.setStoreList(getStoreList());
			return mapping.findForward("success");
		}catch (Exception e) {
			logger.error(e, e);
			return mapping.findForward("error");
		}
	}
}
